import request from '@/api/request';
import { AxiosPromise } from 'axios';
import {
  BlogCollectionAssociateVO,
  CollectionBlogPageQuery,
  CollectionBlogPageResult,
  CollectionPageQuery,
  CollectionPageResult,
  CollectionQueryParam,
  CollectionVO
} from './types';

/**
 * 获取合集列表
 */
export function getCollectionList(): AxiosPromise<CollectionVO[]> {
  return request({
    url: '/api/v1/collection/list',
    method: 'get'
  });
}

/**
 * 分页查询合集
 */
export function pageCollection(
  queryParams: CollectionPageQuery
): AxiosPromise<CollectionPageResult> {
  return request({
    url: '/api/v1/collection/page',
    method: 'get',
    params: queryParams
  });
}

/**
 * 根据ID获取合集信息
 */
export function getCollectionById(id: number): AxiosPromise<CollectionVO> {
  return request({
    url: `/api/v1/collection/${id}`,
    method: 'get'
  });
}

/**
 * 获取用户的所有合集
 */
export function getCollectionsByUser(): AxiosPromise<CollectionVO[]> {
  return request({
    url: '/api/v1/collection/user',
    method: 'get'
  });
}

/**
 * 保存合集信息
 */
export function saveCollection(vo: CollectionVO): AxiosPromise<boolean> {
  return request({
    url: '/api/v1/collection',
    method: 'post',
    data: vo
  });
}

/**
 * 修改合集信息
 */
export function updateCollection(vo: CollectionVO): AxiosPromise<boolean> {
  return request({
    url: '/api/v1/collection',
    method: 'put',
    data: vo
  });
}

/**
 * 删除合集信息
 */
export function deleteCollection(ids: number[]): AxiosPromise<boolean> {
  return request({
    url: '/api/v1/collection',
    method: 'delete',
    data: ids
  });
}

/**
 * 导出合集信息
 */
export function exportCollection(queryParams: CollectionQueryParam): AxiosPromise<Blob> {
  return request({
    url: '/api/v1/collection/export',
    method: 'get',
    params: queryParams,
    responseType: 'blob'
  });
}

/**
 * 根据合集ID获取关联的文章（分页）
 */
export function getBlogsByCollectionId(
  queryParams: CollectionBlogPageQuery
): AxiosPromise<CollectionBlogPageResult> {
  return request({
    url: `/api/v1/collection/${queryParams.id}/blogs`,
    method: 'get',
    params: queryParams
  });
}

/**
 * 查询未归纳的文章列表（分页）
 */
export function getUncategorizedBlogs(
  queryParams: CollectionBlogPageQuery
): AxiosPromise<CollectionBlogPageResult> {
  return request({
    url: '/api/v1/collection/uncategorized-blogs',
    method: 'get',
    params: queryParams
  });
}

/**
 * 关联博客和合集
 */
export function associateBlogWithCollection(vo: BlogCollectionAssociateVO): AxiosPromise<boolean> {
  return request({
    url: '/api/v1/blog-collection/associate',
    method: 'post',
    data: vo
  });
}
